{{ define "content" }}

<form action="" method="post" onsubmit="return form_post();">
<div class="nav-title">
    <a href="/">{{.SiteCf.Name}}</a> &raquo;
    <select id="select-cid" name="select-cid">
        {{range $_, $item := .MainNodes}}
    <option value="{{$item.Id}}" {{if eq $item.Id $.Cobj.Id}}selected="selected"{{end}}>{{$item.Name}}</option>
        {{end}}
    </select>
    - 编辑
</div>

<div class="main-box">
    <p><input id="id-title" type="text" name="title" value="{{.Aobj.Title}}" class="sll" /></p>
    <p><textarea id="id-content" name="content" class="mll tall">{{.Aobj.Content}}</textarea></p>
    <p><input id="id-tags" type="text" value="{{.Aobj.Tags}}" class="sll" /></p>

    <div class="float-right grey fs12">
        <input id="file_upload" name="file_upload" type="file" multiple="true">
        <div id="file-queue"></div>
        <div class="c"></div>
    </div>
    <div class="c"></div>

    <p>
        <label><input type="checkbox" id="id-closecomment" value="1" {{if .Aobj.CloseComment}}checked="checked"{{end}} /> 关闭评论</label>
        •  <label><a href="/admin/post/edit/{{.Aobj.Id}}?act=del" onclick="javascript:return confirm('您确定要删除吗?')">永久删除帖子</a></label>
    </p>

    <p><div class="float-left">
        <input id="btn-preview" type="button" value=" 预 览 " name="submit" class="textbtn" />
        <input id="btn-submit" type="submit" value=" 提 交 " name="submit" class="textbtn" />
    </div><div class="c"></div></p>

    <p>clientIP: {{.Aobj.ClientIp}}</p>

    <div id="id_preview" class="topic-content"></div>

</div>
</form>

<script>
    $("#btn-preview").on("click", function(){
        var content = $("#id-content").val();
        if(content){
            $(this).attr("disabled", true);
            $.ajax({
                type: "POST",
                url: "/content/preview",
                data: JSON.stringify({"act": "preview", "content": content}),
                dataType: "json",
                contentType: "application/json",
                success: function(data){
                    if(data.retcode==200) {
                        $("#id_preview").html(data.html);
                        $("#id_preview").addClass("previews");
                        $("#id_preview pre code").each(function (i, block) {
                            hljs.highlightBlock(block);
                        });
                    }else{
                        $.toast(data.retmsg)
                    }
                    $("#btn-preview").attr("disabled", false);
                },
                failure: function(errMsg) {
                    $.toast(errMsg);
                    $("#btn-preview").attr("disabled", false);
                }
            });
        }else{
            $("#id-content").focus();
        }
    });

    function form_post(){
        var selectid = $("#select-cid").val();
        var title = $("#id-title").val();
        var content = $("#id-content").val();
        var tags = $("#id-tags").val();
        var closecomment = $("#id-closecomment:checked").val();
        if(title){
            $("#btn-submit").attr("disabled", true);
            $.ajax({
                type: "POST",
                url: "/admin/post/edit/{{.Aobj.Id}}",
                data: JSON.stringify({"act": "submit", "cid": parseInt(selectid, 10), "title": title, "content": content, "tags": tags, "closecomment": closecomment}),
                dataType: "json",
                contentType: "application/json",
                success: function(data){
                    if(data.retcode == 200){
                        window.location.href = "/t/"+data.aid;
                        return
                    }
                    $.toast(data.retmsg);
                    $("#btn-submit").attr("disabled", false);
                },
                failure: function(errMsg) {
                    $.toast(errMsg);
                    $("#btn-submit").attr("disabled", false);
                }
            });
        }else{
            if(!title){
                $("#id_title").focus();
            }
        }
        return false;
    }

    $(function() {
        $('#file_upload').uploadifive({
            'auto': true,
            'queueID': 'file-queue',
            'checkScript': false,
            'fileSizeLimit': {{.SiteCf.UploadMaxSizeByte}},
            'multi': true,
            'uploadLimit': 10,
            'queueSizeLimit': 10,
            'buttonText': "上传文件",
            'fileType': false,
            'fileObjName': 'file',
            'removeCompleted': true,
            'truncateLength': 30,
            'height': 22,
            'width': 80,
            'uploadScript': '/file/upload',
            'onUploadComplete': function (file, data) {
                console.log(file);
                var rsp = jQuery.parseJSON(data);
                if(rsp.retcode == 200) {
                    var con = document.getElementById("id-content").value;
                    document.getElementsByTagName("textarea")[0].focus();
                    document.getElementById("id-content").value = con + "\n"+rsp.url+"\n";
                }else{
                    $.toast(rsp.retmsg);
                }
            },
            'onError': function (errorType) {
                $.toast('The error was: ' + errorType);
            }
        });
    });
</script>


{{ end}}

